﻿@{
    Uri url = new Uri(Request.Url.AbsoluteUri);
    ViewBag.Server = url.GetLeftPart(UriPartial.Authority);
    string crudServiceBaseUrl = ViewBag.Server + "/api/";
    
    ViewBag.Title = "Choose Task";
}


<div class="centercontent">
        @using (Html.BeginForm("EditEntity", "EntityEdit"))
    {
        @Html.AntiForgeryToken()
    <div>
        <input type="text" name="EntityID" />
        <button type="submit" name="commandctrl" value="RefreshEntity">Edit This Entity</button>
    </div>
    <div>
        @ViewBag.Feedback
    </div>
    }

    <div style="border:double" id="BINDDIV-template" data-template ="entity-template"
                          data-bind="source: entityDataSource">
        @*<ul id="simple-template" class="relations"
            data-role="listview"
            data-template="entity-template"
            data-bind="source: entityDataSource"></ul>*@
    </div>
</div>

<script type="text/x-kendo-tmpl" id="entity-template">
    <div>
    @if (ViewBag.AttList != null)
    {
        foreach (var item in ViewBag.AttList)
        {
            if (item.Editable == true)
            {
                if (item.AttributeType == "String")
                {
                    <div>@item.AttributeName (@item.AttributeType): <input id="@item.AttributeName" data-bind="value: @item.AttributeName" /></div>
                }
            }
            else
            {
                <div>@item.AttributeName: <span>#:@item.AttributeName#</span></div>
            }
        }
        <button onclick="updateData()" >Save Changes</button>
        <button onclick="cancelUpdate()">Cancel</button>

    }
    </div>
</script>

<script>
    EditObservable = kendo.observable({
        EntityID: "@(ViewBag.EntityID)",
        EntityName: '',
        IsRendered: "false",
        entityDataSource: new kendo.data.DataSource({
            transport: {
                read: {
                    url: '',
                    dataType: "json"
                },
                update: {
                    url: "",
                    dataType: "json",
                    type: "put"
                },
                parameterMap: function (options, operation) {
                    if (operation !== "read" && options.models) {
                        return { models: kendo.stringify(options.models) };
                    }
                }
            },
            batch: true,
            requestEnd: function (e) {

                setTimeout(function () {
                    @*if(@(ViewBag.VMName).entityRelationshipDataSource.transport.options.read.url == "") {
                    @(ViewBag.VMName).entityRelationshipDataSource.transport.options.read.url = "@crudServiceBaseUrl" + "readnew/" + "@(ViewBag.ChainSoFar)" + "/relatedentities?TD=@(ViewBag.td)";
                    @(ViewBag.VMName).entityRelationshipDataSource.fetch();
                }*@
                })
            },
            schema: {
                model: {
                    id: "EntityID"
                }
            }
            //,
            //change: function (e) {
            //    alert(EditObservable.entityDataSource.hasChanges());
            //    if (EditObservable.entityDataSource.hasChanges())
            //        EditObservable.entityDataSource.sync();
            //}
        })
    });

    function updateData() {
        if (EditObservable.entityDataSource.hasChanges())
            EditObservable.entityDataSource.sync();
    }
    function cancelUpdate() {
        EditObservable.entityDataSource.cancelChanges();
    }


    $(function () {
        kendo.bind($("#BINDDIV-template"), EditObservable);
        // Polyfill for IE
        PointerEventsPolyfill.initialize({});
        EditObservable.entityDataSource.transport.options.read.url = "@crudServiceBaseUrl"
                + 'readnew/' + "@(ViewBag.EntityTypeSubType)" + '?Filter=EntityID="@(ViewBag.EntityID)"';
        EditObservable.entityDataSource.transport.options.update.url = "@crudServiceBaseUrl"
                + 'write/Entity/@(ViewBag.EntityID)';
        EditObservable.entityDataSource.fetch(function () {

            var data = EditObservable.entityDataSource.at(0);
            EditObservable.set("EntityName", data.EntityName);
        });
    });

</script>


